from OpenGL.GL import *
import pyglet

from common import *
from common.sounds import *
from game.core.gamemanager import GameManager

class MainMenu(object):
	"""
	This class represents the game's Main Menu.
	"""
	
	START = 0
	LOAD = 1
	EXIT = 2
	
	def __init__(self):
		# Creates the logo sprite
		img = pyglet.image.load('../data/images/main_menu/logo.png')
		img.anchor_x = img.width / 2
		self.logo = pyglet.sprite.Sprite(img,
							x=GameManager.main_window.win.width / 2,
							y=GameManager.main_window.win.height / 2 + 200)
		
		# Creates the menu items
		self.items = []
		img = pyglet.image.load('../data/images/main_menu/start.png')
		img.anchor_x = img.width / 2
		img.anchor_y = img.height / 2
		self.items.append(MenuItem(60, img, MainMenu.START))
		img = pyglet.image.load('../data/images/main_menu/load.png')
		img.anchor_x = img.width / 2
		img.anchor_y = img.height / 2
		self.items.append(MenuItem(-60, img, MainMenu.LOAD))
		img = pyglet.image.load('../data/images/main_menu/exit.png')
		img.anchor_x = img.width / 2
		img.anchor_y = img.height / 2
		self.items.append(MenuItem(-180, img, MainMenu.EXIT))
		
		img = pyglet.image.load('../data/images/not_implemented.png')
		img.anchor_y = img.height
		self.notImplemented = pyglet.sprite.Sprite(img)
		self.showNotImplemented = False
		
		self.credits = pyglet.text.Label(
						'Rafael Dahis, Rafael Kikuchi, Talita Gomes - 2009',
						font_name='Verdana',
						font_size=11, x=50, y=50)  
		
		self.hovered = None
		self.clicked = False
	
	def on_mouse_motion(self, x, y, dx, dy):
		if not(self.hovered is None):
			if self.hovered.isInside(x, y):
				return
			else:
				self.hovered.status = MenuItem.UNSELECTED
				self.hovered = None
		
		for i in self.items:
			if i.isInside(x, y) and i.status == MenuItem.UNSELECTED:
				if self.clicked:
					i.status = MenuItem.CLICKED
				else:
					i.status = MenuItem.HOVERED
				self.hovered = i
				break
	
	def on_mouse_press(self, x, y, button, modifiers):
		if not(self.hovered is None):
			self.hovered.status = MenuItem.CLICKED
		self.clicked = True
		self.showNotImplemented = False
		GameManager.play_sound('menu_click')

	def on_mouse_release(self, x, y, button, modifiers):
		if not(self.hovered is None):
			if self.hovered.isInside(x, y):
				self.hovered.status = MenuItem.HOVERED
				if self.hovered.id == MainMenu.START:
					GameManager.change_state(GameManager.PLAYING)
				elif self.hovered.id == MainMenu.LOAD:
					self.notImplemented.x = x
					self.notImplemented.y = y
					self.showNotImplemented = True
				elif self.hovered.id == MainMenu.EXIT:
					GameManager.main_window.quit()
			else:
				self.hovered.status = MenuItem.UNSELECTED
				self.hovered = None
			
		self.clicked = False
	
	def on_draw(self):
		GameManager.main_window.win.clear()
		
		glDisable(GL_LIGHTING)
		self.logo.draw()
		for i in self.items:
			i.render()
		if self.showNotImplemented:
			self.notImplemented.draw()
		self.credits.draw()
		glEnable(GL_LIGHTING)
	
class MenuItem(object):
	"""
	This class represents a main menu item. 
	"""
	
	# Possible menu item status.
	UNSELECTED = 0
	HOVERED = 1
	CLICKED = 2
	
	# Images related to items
	img_unselected = pyglet.image.load('../data/images/main_menu/item_unselected.png')
	img_unselected.anchor_x = img_unselected.width / 2
	img_unselected.anchor_y = img_unselected.height / 2
	
	img_hovered = pyglet.image.load('../data/images/main_menu/item_hovered.png')
	img_hovered.anchor_x = img_hovered.width / 2
	img_hovered.anchor_y = img_hovered.height / 2
	
	img_clicked = pyglet.image.load('../data/images/main_menu/item_clicked.png')
	img_clicked.anchor_x = img_clicked.width / 2
	img_clicked.anchor_y = img_clicked.height / 2
	
	def __init__(self, offset, img_label, id):
		x = GameManager.main_window.win.width / 2
		y = GameManager.main_window.win.height / 2 + offset
		imgSize = [MenuItem.img_unselected.width, MenuItem.img_unselected.height]
		
		self.unselected_sprite = pyglet.sprite.Sprite(MenuItem.img_unselected, x, y)
		self.hovered_sprite = pyglet.sprite.Sprite(MenuItem.img_hovered, x, y)
		self.clicked_sprite = pyglet.sprite.Sprite(MenuItem.img_clicked, x, y)
		
		self.status = MenuItem.UNSELECTED
		self.boundings = [x - imgSize[X] / 2, y - imgSize[Y] / 2,
						x + imgSize[X] / 2, y + imgSize[Y] / 2]
		
		self.label = pyglet.sprite.Sprite(img_label, x, y)
		self.id = id
		
	def render(self):
		if self.status == MenuItem.UNSELECTED:
			self.unselected_sprite.draw()
		elif self.status == MenuItem.HOVERED:
			self.hovered_sprite.draw()
		elif self.status == MenuItem.CLICKED:
			self.clicked_sprite.draw()
		self.label.draw()
			
	def isInside(self, x, y):
		"""
		States if the point (x,y) is over the button. 
		"""
		
		if x > self.boundings[0] and x < self.boundings[2] and \
			y > self.boundings[1] and y < self.boundings[3]:
			return True
		
		return False
	